package com.youpai.platform.order.service.convert;

import com.youpai.platform.biggod.service.entity.po.SkillPO;
import com.youpai.platform.chat.service.entity.po.ChatAccountPO;
import com.youpai.platform.order.service.entity.po.OrderPO;
import com.youpai.platform.order.web.entity.dto.OrderDto;
import com.youpai.platform.order.web.entity.dto.OrderListDto;
import com.youpai.platform.order.service.entity.po.OrderEvaluatePO;
import com.youpai.platform.order.web.entity.dto.EvaluateDto;
import com.youpai.platform.user.service.entity.po.UserPO;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author huanghao
 * @version 1.0
 * @description 订单相关DTO转换
 * @date 2022/11/15
 */
public class OrderConverter {

    //将品类po转为dto
    public static OrderDto convertOrderPOToOrderDto(final OrderPO orderPO, final ChatAccountPO chatAccountPO,
                                                    final UserPO userPO, final SkillPO skillPO) {
        OrderDto orderDto = new OrderDto();
        orderDto.setOrderId(orderPO.getOrderId().toString())
                .setUid(userPO.getUid().toString())
                .setUserName(userPO.getUserName())
                .setAccId(chatAccountPO.getAccId())
                .setSkillId(skillPO.getSkillId().toString())
                .setSkillName(skillPO.getSkill())
                .setCount(orderPO.getCount())
                .setPrice(orderPO.getPrice())
                .setSumPrice(orderPO.getSumPrice())
                .setOrderStatus(orderPO.getOrderStatus())
                .setCreateTime(orderPO.getCreateTime().getTime())
                .setUpdateTime(orderPO.getUpdateTime().getTime());
        return orderDto;
    }

    public static List<OrderListDto> convertOrderPOsToOrderDtos(List<OrderPO> orderPOS,
                                                                Map<Long, String> skillNameMap, Map<Long, String> godNameMap, Map<Long, OrderEvaluatePO> orderEvaluatePOMap) {
        List<OrderListDto> orderListDtos = new ArrayList<>();
        for (OrderPO orderPO : orderPOS) {
            OrderListDto orderListDto = convertOrderPOsToOrderListDto(
                    orderPO, skillNameMap.get(orderPO.getSkillId()),
                    godNameMap.get(orderPO.getBigGodUid()),
                    orderEvaluatePOMap.get(orderPO.getOrderId()));
            orderListDtos.add(orderListDto);
        }
        return orderListDtos;
    }

    private static OrderListDto convertOrderPOsToOrderListDto(OrderPO orderPO, String skillName, String godName, OrderEvaluatePO orderEvaluatePO) {
        //订单评价可能为null
        Double star = null;
        if(orderEvaluatePO != null){
            star = orderEvaluatePO.getStar();
        }
        OrderListDto orderListDto = new OrderListDto();
        orderListDto.setOrderId(orderPO.getOrderId().toString())
                .setSkillId(orderPO.getSkillId().toString())
                .setGodUid(orderPO.getBigGodUid().toString())
                .setGodNickName(godName)
                .setStar(star)
                .setSkillName(skillName)
                .setCount(orderPO.getCount())
                .setPrice(orderPO.getPrice())
                .setSumPrice(orderPO.getSumPrice())
                .setOrderStatus(orderPO.getOrderStatus())
                .setCreateTime(orderPO.getCreateTime().getTime())
                .setUpdateTime(orderPO.getUpdateTime().getTime());
        if (null != orderEvaluatePO) {
            orderListDto.setStar(orderEvaluatePO.getStar());
        }
        return orderListDto;
    }

    //将订单评价po转为dto
    public static EvaluateDto convertOrderEvaluatePOToEvaluateDTO(final OrderEvaluatePO orderEvaluatePO, String userName) {
        EvaluateDto evaluateDto = new EvaluateDto();
        evaluateDto.setUid(orderEvaluatePO.getUserUid().toString())
                .setUserName(userName)
                .setEvaluate(orderEvaluatePO.getEvaluate())
                .setStar(orderEvaluatePO.getStar().intValue());
        return evaluateDto;
    }

    //将订单评价po列表转为dto列表
    public static List<EvaluateDto> convertOrderEvaluatePOToEvaluateDTO(List<OrderEvaluatePO> orderEvaluatePOS, Map<Long, String> userNameMap) {
        if (CollectionUtils.isEmpty(orderEvaluatePOS)) {
            return Collections.emptyList();
        }
        List<EvaluateDto> evaluateDtoList = new ArrayList<>(orderEvaluatePOS.size());
        for (OrderEvaluatePO orderEvaluatePO : orderEvaluatePOS) {
            evaluateDtoList.add(convertOrderEvaluatePOToEvaluateDTO(orderEvaluatePO, userNameMap.get(orderEvaluatePO.getUserUid())));
        }
        return evaluateDtoList;
    }
}
